package com.service;

import com.dao.UserDao;
import com.entity.Users;

import com.util.SafeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户service
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 通过用户名获取
     */
    public  Users getByUsername(String username){
    return  userDao.selectByUsername(username);
}

    public boolean add(Users user){
        user.setPassword(SafeUtil.encode(user.getPassword()));
        return userDao.insert(user);
    }


    public Users getByUsernameAndPassword(String username, String password) {
        return userDao.selectByUsernameAndPassword(username, SafeUtil.encode(password));
    }

    public boolean update(int id,String name,String phone,String address){
        Users user=new Users();
        user.setId(id);
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);
        return userDao.update(user);
    }

    public Users get(int id){
        return userDao.select(id);
    }

    /**
     * 修改密码
     * @param id
     * @param password
     * @return
     */
    public boolean updatePassword(int id,String password){
        return userDao.updatePassword(id,SafeUtil.encode(password));
    }

    public List<Users> getList(){
        return userDao.selectList();
    }
}
